<template>
	<div>
		<van-nav-bar left-text="返回" left-arrow @click-left="onClickLeft" />
		<div v-show="alterInfo === false">
			<div class="shop-info">
				<p style="font-weight: bolder">本店名称：</p>
				<p>{{shop.shopName}}</p>
			</div>
			<div class="shop-info">
				<p style="font-weight: bolder">本店地址：</p>
				<p>{{shop.shopAddress}}</p>
			</div>
			<div class="shop-info">
				<p style="font-weight: bolder">联系电话：</p>
				<p>{{shop.shopPhone}}</p>
			</div>
			<div class="shop-info">
				<p style="font-weight: bolder">我的微信：</p>
				<p>{{shop.shopWeiXin}}</p>
			</div>
			<van-button round type="primary" @click="alterInfo = true" style="width: 100%;">修改本店信息</van-button>
		</div>
		<div v-show="alterInfo === true">
			<van-form @submit="onSubmit">
				<van-field v-model="shop.shopName" name="shopName" label="本店名称" />
				<van-field v-model="shop.shopAddress" name="shopAddress" label="本店地址" />
				<van-field v-model="shop.shopPhone" name="shopPhone" label="联系电话" />
				<van-field v-model="shop.shopWeiXin" name="shopWeiXin" label="我的微信" />
				<van-button round type="info" native-type="submit" style="width: 50%;">确认修改本店信息</van-button>
			</van-form>
			<van-button round type="danger" @click="alterInfo = false" style="width: 50%;">取消修改</van-button>
		</div>
	</div>
</template>

<script>
	import {
		Notify
	} from 'vant';

	export default {
		data() {
			return {
				alterInfo: false,
				user: {},
				shop: {}
			}
		},
		methods: {
			onClickLeft() {
				this.$router.back()
			},
			onSubmit(values) {
				console.log(values);
				this.$axios({
					method: 'post',
					url: '/shop/updateShopInfo',
					headers: {
						'UserId': this.user.userId,
						'CarToken': this.user.token
					},
					data: this.$qs.stringify({
						shopId: this.shop.shopId,
						shopName: this.shop.shopName,
						shopAddress: this.shop.shopAddress,
						shopPhone: this.shop.shopPhone,
						shopWeiXin: this.shop.shopWeiXin
					})
				}).then((resp) => {
					if (resp.data.msg == "修改信息成功") {
						Notify({
							type: 'success',
							message: '修改成功'
						});
						window.location.reload();
					} else if (resp.data.status == 500) {
						Notify({
							type: 'danger',
							message: '未登录或登录已失效，请重新登录'
						})
						this.$router.back()
					}
				})
			}
		},
		mounted() {
			let user = this.$cookies.get('user');

			if (user != null) {
				this.user = user;
				this.$axios({
					method: 'post',
					url: '/shop/findShopInfo',
					headers: {
						'UserId': this.user.userId,
						'CarToken': this.user.token
					},
					data: this.$qs.stringify({
						shopId: this.user.shopId
					})
				}).then((resp) => {
					if (resp.data.status == 200) {
						this.shop = resp.data.obj;
					} else if (resp.data.status == 500) {
						Notify({
							type: 'danger',
							message: '未登录或登录已失效，请重新登录'
						})
						this.$router.back()
					}
				})
			}

		}
	}
</script>

<style scoped>
	.shop-info {
		display: flex;
	}
</style>
